package control;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import entities.tools.FilledForm;
import entities.tools.Form;
import entities.users.FormOwner;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.ServletSecurity.TransportGuarantee;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.ejb.FilledFormBeanInterface;
import util.ejb.FormBeanInterface;
import util.ejb.FormOwnerBeanInterface;

/**
 *
 * @author Marek
 */
@WebServlet(urlPatterns = {"/formOwners.jsp"})
@ServletSecurity(
        @HttpConstraint(transportGuarantee = TransportGuarantee.CONFIDENTIAL, rolesAllowed = {"admin"}))
public class FormOwnersServlet extends AbstractServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(true);

        if (null != session.getAttribute("getParamDelete")) {
            Long formOwnerId = new Long((String) session.getAttribute("getParamDelete"));
            formOwnerBean.deleteFormOwner(formOwnerBean.getFormOwner(formOwnerId));
        }
        if (null != session.getAttribute("getParamSelect")) {
            Long formOwnerId = new Long((String) session.getAttribute("getParamSelect"));
            FormOwner formOwner = formOwnerBean.getFormOwner(formOwnerId);

            Map<String, String[]> requestParameters = request.getParameterMap();
            if (requestParameters.containsKey("firstName")
                    && requestParameters.containsKey("secondName")
                  && requestParameters.containsKey("phone")
                    && requestParameters.containsKey("email")
                    ) {
                formOwner.setFirstName(requestParameters.get("firstName")[0]);
                formOwner.setSecondName(requestParameters.get("secondName")[0]);
                formOwner.setPhone(requestParameters.get("phone")[0]);
                formOwner.setEmail(requestParameters.get("email")[0]);
                formOwnerBean.updateFormOwner(formOwner);
            } else {}
            session.setAttribute("formOwner", formOwner);
        }
        if (null != session.getAttribute("getParamGenerate") && session.getAttribute("getParamGenerate").equals("true")) {
            formOwnerBean.createFormOwner();
        }
        List<FormOwner> forms = formOwnerBean.getFormOwners();
        session.setAttribute("formOwners", forms);
        request.getRequestDispatcher("WEB-INF/formOwnersTmpl.jsp").include(request, response);
    }
}

